clear,clc

s=zeros(9);
rcn=input('rcn='); % rcn=[r1c1n1 r2c2n2 ... rncnnn]
r=fix(rcn/100);
c=mod(fix(rcn/10),10);
n=mod(rcn,10);
rcn=(c-1)*9+r;
s(rcn)=n;
disp(s)

t=repmat(0:9,81,1);
t(rcn)=n;
t(rcn,2:10)=0;
for i=1:size(rcn,2)
    t(i,t(i)+1)=t(i);
end

rc=zeros(3);
for i=1:3
    for j=1:3
        rc(i,j)=(j-1)*27+i*3-2;
    end
end

p=0;
ss=s;
tt=s;
while 1
    while sum(sum(tt-ss)) && sum(sum(tt==0))
        p=0;
        rcn=find((tt-s)>0);
        ss=tt;
        for i=1:9
            for j=1:9
                if sum(t(i:9:81,j+1)>0)==1
                    k=(find(t(i:9:81,j+1)>0)-1)*9+i;
                    t(k)=j;
                    t(k,2:10)=0;
                    t(k,j+1)=j;
                end
            end
        end
        
        for i=1:9
            for j=1:9
                if sum(t(i*9-8:i*9,j+1)>0)==1
                    k=(i-1)*9+find(t(i*9-8:i*9,j+1)>0);
                    t(k)=j;
                    t(k,2:10)=0;
                    t(k,j+1)=j;
                end
            end
        end
    
        for i=1:9
            for j=1:9
                if sum(t([rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20],j+1)>0)==1
                    k=[rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20];
                    kk=find(t([rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20],j+1)>0);
                    k=k(kk);
                    t(k)=j;
                    t(k,2:10)=0;
                    t(k,j+1)=j;
                end
            end
        end
        
        tt=reshape(t(1:81),[9 9]);
        disp(tt)
    end
    
    if p
        break
    end
    
    s=ss;
    c=ceil(rcn/9);
    r=rcn-(c-1)*9;
    rr=ceil(r/3);
    cc=ceil(c/3);
    for i=1:max(size(rcn))
        t(r(i):9:end,s(rcn(i))+1)=0;
        t((c(i)-1)*9+1:c(i)*9,s(rcn(i))+1)=0;
        for j=0:2
            t(rc(rr(i),cc(i))+j*9:rc(rr(i),cc(i))+j*9+2,s(rcn(i))+1)=0;
        end
        t(rcn(i),s(rcn(i))+1)=s(rcn(i));
    end

    for i=1:81
        if sum(t(i,2:10)>0)==1
            t(i)=sum(t(i,2:10));
            t(i,2:10)=0;
            t(i,t(i)+1)=t(i);
        end
    end
    
    ss=reshape(t(1:81),[9 9]);
    disp(ss)
    
    if sum(sum(ss>0))==81
        break
    end
    
    for i=1:9
        for j=1:9
            if sum(t(i:9:81,j+1)>0)==1
                k=(find(t(i:9:81,j+1)>0)-1)*9+i;
                t(k)=j;
                t(k,2:10)=0;
                t(k,j+1)=j;
            end
        end
    end
    
    for i=1:9
        for j=1:9
            if sum(t(i*9-8:i*9,j+1)>0)==1
                k=(i-1)*9+find(t(i*9-8:i*9,j+1)>0);
                t(k)=j;
                t(k,2:10)=0;
                t(k,j+1)=j;
            end
        end
    end
    
    for i=1:9    
        for j=1:9
            if sum(t([rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20],j+1)>0)==1
                k=[rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20];
                kk=find(t([rc(i):rc(i)+2 rc(i)+9:rc(i)+11 rc(i)+18:rc(i)+20],j+1)>0);
                k=k(kk);
                t(k)=j;
                t(k,2:10)=0;
                t(k,j+1)=j;
            end
        end
    end
    
    tt=reshape(t(1:81),[9 9]);
    disp(tt)
    
    p=1;
end